from selenium.webdriver.common.by import By

from quote.base.usebrowser import UseBrowser


class BrowserOperation:

    def __init__(self,driver):
        self.driver=driver

    def open_url(self,url):
        self.driver.get(url)

    def find_element_p(self, type, locator):
        # 将type转换为大写
        type_upper = type.upper()
        # 使用getattr获取By类中的对应属性
        by_type = getattr(By, type_upper)
        # 使用获取到的by_type进行元素定位
        element = self.driver.find_element(by_type, locator)
        return element

    def input_text(self, type, locator,text):
        self.find_element_p(type,locator).send_keys(text)


    def click_element(self,type,locator):
        self.find_element_p(type, locator).click()

    #获取文本
    def get_text(self,type,locator):
        return self.find_element_p(type,locator).text

    #切换页面
    def change_frame(self,name):
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(name)
if __name__=='__main__':
    ub=UseBrowser()
    bo=BrowserOperation(ub.driver)
    bo.open_url("http://localhost:8080/JavaPrj_6/")
    bo.input_text('name','username','admin')
    bo.input_text('name','password','admin')
    bo.click_element('name','submit')